<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { getOverviewApi } from "@/api/home";
import { ElMessage } from "element-plus";

const router = useRouter();

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0,
});

// 快捷操作导航配置
const quickActions = [
  {
    title: "课程管理",
    subtitle: "课程信息维护",
    route: "/resource/course",
    icon: "Reading",
  },
  {
    title: "活动管理",
    subtitle: "营销活动管理",
    route: "/resource/activity",
    icon: "Promotion",
  },
  {
    title: "线索管理",
    subtitle: "潜在客户追踪",
    route: "/clue/list",
    icon: "User",
  },
  {
    title: "商机管理",
    subtitle: "销售机会管理",
    route: "/business/list",
    icon: "Opportunity",
  },
  {
    title: "客户管理",
    subtitle: "客户信息维护",
    route: "/customer",
    icon: "Avatar",
  },
  {
    title: "日志统计",
    subtitle: "系统操作日志",
    route: "/system/log",
    icon: "Document",
  },
];

// 获取概览数据
const getOverview = async () => {
  try {
    const result = await getOverviewApi();
    if (result.code === 1) {
      overviewData.value = result.data;
    } else {
      ElMessage.error(result.msg || "获取数据失败");
    }
  } catch (error) {
    ElMessage.error("网络请求失败");
  }
};

// 跳转到对应页面
const navigateTo = (route) => {
  router.push(route);
};

onMounted(() => {
  getOverview();
});
</script>

<template>
  <div class="home-container">
    <!-- 欢迎标题 -->
    <div class="welcome-section">
      <h1 class="welcome-title">欢迎您, 进入轻客管家系统</h1>
    </div>

    <!-- 概览数据区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card">
        <h3 class="card-title">线索概览</h3>
        <div class="metrics-grid">
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueTotal }}</div>
            <div class="metric-label">总线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueWaitAllot }}</div>
            <div class="metric-label">待分配线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueWaitFollow }}</div>
            <div class="metric-label">待跟进线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueFollowing }}</div>
            <div class="metric-label">跟进中线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.clueFalse }}</div>
            <div class="metric-label">伪线索数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">
              {{ overviewData.clueConvertBusiness }}
            </div>
            <div class="metric-label">转商机线索数</div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card">
        <h3 class="card-title">商机概览</h3>
        <div class="metrics-grid">
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessTotal }}</div>
            <div class="metric-label">总商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessWaitAllot }}</div>
            <div class="metric-label">待分配商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">
              {{ overviewData.businessWaitFollow }}
            </div>
            <div class="metric-label">待跟进商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessFollowing }}</div>
            <div class="metric-label">跟进中商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">{{ overviewData.businessFalse }}</div>
            <div class="metric-label">回收商机数</div>
          </div>
          <div class="metric-item">
            <div class="metric-value">
              {{ overviewData.businessConvertCustomer }}
            </div>
            <div class="metric-label">转客户商机数</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions-section">
      <div class="quick-actions-grid">
        <div
          v-for="action in quickActions"
          :key="action.route"
          class="action-card"
          @click="navigateTo(action.route)"
        >
          <div class="action-icon">
            <el-icon :size="32">
              <component :is="action.icon" />
            </el-icon>
          </div>
          <div class="action-title">{{ action.title }}</div>
          <div class="action-subtitle">{{ action.subtitle }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100%;
}

.welcome-section {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-title {
  font-size: 28px;
  color: #303133;
  font-weight: 500;
  margin: 0;
}

.overview-section {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.overview-card {
  flex: 1;
  background-color: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
}

.card-title {
  font-size: 18px;
  color: #303133;
  margin: 0 0 20px 0;
  font-weight: 500;
  text-align: center;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.metric-item {
  text-align: center;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

.metric-value {
  font-size: 24px;
  font-weight: 600;
  color: #409eff;
  margin-bottom: 8px;
}

.metric-label {
  font-size: 12px;
  color: #606266;
  line-height: 1.4;
}

.quick-actions-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
}

.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 20px;
}

.action-card {
  text-align: center;
  padding: 24px 16px;
  background-color: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-card:hover {
  background-color: #e6f7ff;
  border-color: #91d5ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.15);
}

.action-icon {
  margin-bottom: 16px;
  color: #409eff;
}

.action-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 8px;
}

.action-subtitle {
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .quick-actions-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .overview-section {
    flex-direction: column;
  }

  .quick-actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .metrics-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
